/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentation;

import BLO.DoibongBLO;
import BLO.HLVBLO;
import BLO.SanBLO;
import DAO.ResultsModel;
import DTO.DOIBONG;
import DTO.HLV;
import DTO.NGUOI;
import DTO.SAN;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author LUCKYSTARS
 */
public class QLDB extends javax.swing.JPanel {
    DoibongBLO dbBLO= new DoibongBLO();
    SanBLO sBLO= new SanBLO();
    HLVBLO hlvBLO= new HLVBLO();
    
    /**
     * Creates new form QLDB
     */
    public QLDB() {
        
        initComponents();
        
        try {
            
            //Dua du lieu vao combobox San
            ResultSet dtSan=sBLO.Getdata();
            while (dtSan.next()) 
            {
            cmbSan.addItem(dtSan.getString("Ten_SAN"));
            }
            //Dua du lieu vao Combobox HLV
            ResultSet dtHlv=hlvBLO.Getdata();
            while (dtHlv.next()) 
            {
            cmbHLV.addItem(dtHlv.getString("HO_TEN"));
            }
            
            
            //Hien thi datagridview
            ResultsModel dtable= dbBLO.Getdata();
            JTable tb= new JTable(dtable);
            tb.setBounds(0,0,869,300);
            JScrollPane js= new JScrollPane(tb);
            js.setBounds(0,0,869,300);
            dtgDoibong.add(js);
            ////Dang ky su kien Selection Change
            SelectionListener listener = new SelectionListener(tb);
            tb.getSelectionModel().addListSelectionListener(listener);
              
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage() );
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtMadoibong = new javax.swing.JTextField();
        txtTendoibong = new javax.swing.JTextField();
        cmbHLV = new javax.swing.JComboBox();
        cmbSan = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        btnDangky = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnTrove = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        dtgDoibong = new javax.swing.JPanel();

        jButton1.setText("jButton1");

        setPreferredSize(new java.awt.Dimension(869, 533));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel2.setText("Mã đội bóng");

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel3.setText("Huấn luyện viên");

        jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel4.setText("Sân");

        jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel5.setText("Tên đội bóng");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtMadoibong, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                    .addComponent(cmbHLV, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(45, 45, 45)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtTendoibong, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                    .addComponent(cmbSan, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5)
                    .addComponent(txtMadoibong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTendoibong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(cmbHLV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbSan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnDangky.setText("Đăng ký");
        btnDangky.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDangkyActionPerformed(evt);
            }
        });

        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnTrove.setText("Trở về");
        btnTrove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTroveActionPerformed(evt);
            }
        });

        btnRefresh.setText("Refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(btnDangky, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(75, 75, 75)
                .addComponent(btnSua, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(85, 85, 85)
                .addComponent(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(86, 86, 86)
                .addComponent(btnTrove, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSua, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnDangky, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnXoa)
                    .addComponent(btnRefresh, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnTrove, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(14, 14, 14))
        );

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jLabel1.setText("QUẢN LÝ ĐỘI BÓNG");

        dtgDoibong.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout dtgDoibongLayout = new javax.swing.GroupLayout(dtgDoibong);
        dtgDoibong.setLayout(dtgDoibongLayout);
        dtgDoibongLayout.setHorizontalGroup(
            dtgDoibongLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 845, Short.MAX_VALUE)
        );
        dtgDoibongLayout.setVerticalGroup(
            dtgDoibongLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 621, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(dtgDoibong, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 309, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addGap(314, 314, 314))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(dtgDoibong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnDangkyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDangkyActionPerformed

        DOIBONG dbDTO= new DOIBONG();
        SAN sDTO= new SAN();
        NGUOI nDTO= new NGUOI();
        HLV hlvDTO= new HLV();
         
        dbDTO.setMaDB(txtMadoibong.getText().trim());
        dbDTO.setTenDB(txtTendoibong.getText().trim());
        //Lay du lieu tu combobox San
        sDTO.setTensan(cmbSan.getSelectedItem().toString().trim());
        sDTO.setMasan(sBLO.GetMasanFTensan(sDTO));
        dbDTO.setSan(sDTO);
        //Lay du lieu tu combobox HLV
        nDTO.setHoten(cmbHLV.getSelectedItem().toString().trim());
        nDTO.setManguoi(hlvBLO.GetMaFTenHLV(nDTO));
        hlvDTO.setNguoi(nDTO);
        dbDTO.setHlv(hlvDTO);

        dbBLO.Insert(dbDTO);
   
           ResultsModel dtable= dbBLO.Getdata();
            JTable tb= new JTable(dtable);
            tb.setBounds(0,0,869,300);
            JScrollPane js= new JScrollPane(tb);
            js.setBounds(0,0,869,300);
            dtgDoibong.removeAll();
            dtgDoibong.add(js);
              SelectionListener listener = new SelectionListener(tb);
              tb.getSelectionModel().addListSelectionListener(listener);
           
        // TODO add your handling code here:
    }//GEN-LAST:event_btnDangkyActionPerformed

    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed

          DOIBONG dbDTO= new DOIBONG();
        SAN sDTO= new SAN();
        NGUOI nDTO= new NGUOI();
        HLV hlvDTO= new HLV();
         
        dbDTO.setMaDB(txtMadoibong.getText().trim());
        dbDTO.setTenDB(txtTendoibong.getText().trim());
        //Lay du lieu tu combobox San
        sDTO.setTensan(cmbSan.getSelectedItem().toString().trim());
        sDTO.setMasan(sBLO.GetMasanFTensan(sDTO));
        dbDTO.setSan(sDTO);
        //Lay du lieu tu combobox HLV
        nDTO.setHoten(cmbHLV.getSelectedItem().toString().trim());
        nDTO.setManguoi(hlvBLO.GetMaFTenHLV(nDTO));
        hlvDTO.setNguoi(nDTO);
        dbDTO.setHlv(hlvDTO);
        
         dbBLO.Update(dbDTO);
            ResultsModel dtable= dbBLO.Getdata();
            JTable tb= new JTable(dtable);
            tb.setBounds(0,0,869,300);
            JScrollPane js= new JScrollPane(tb);
            js.setBounds(0,0,869,300);
            dtgDoibong.removeAll();
            dtgDoibong.add(js);
            
              SelectionListener listener = new SelectionListener(tb);
              tb.getSelectionModel().addListSelectionListener(listener);
        // TODO add your handling code here:
    }//GEN-LAST:event_btnSuaActionPerformed

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        
          DOIBONG dbDTO= new DOIBONG();
          dbDTO.setMaDB(txtMadoibong.getText().trim());
          
          dbBLO.Delete(dbDTO);
          //Load lai table
            ResultsModel dtable= dbBLO.Getdata();
            JTable tb= new JTable(dtable);
            tb.setBounds(0,0,869,300);
            JScrollPane js= new JScrollPane(tb);
            js.setBounds(0,0,869,300);
            dtgDoibong.removeAll();
            dtgDoibong.add(js);
            
              SelectionListener listener = new SelectionListener(tb);
              tb.getSelectionModel().addListSelectionListener(listener);
        // TODO add your handling code here:
    }//GEN-LAST:event_btnXoaActionPerformed

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        txtMadoibong.setText("");
        txtTendoibong.setText("");
        cmbHLV.setSelectedIndex(0);
        cmbSan.setSelectedIndex(0);
        
        // TODO add your handling code here:
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void btnTroveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTroveActionPerformed
    this.setVisible(false);
        
        // TODO add your handling code here:
    }//GEN-LAST:event_btnTroveActionPerformed
  class SelectionListener implements ListSelectionListener {
   JTable table;
//Su kien selection change
    SelectionListener(JTable table) {
    this.table = table;}
    public void valueChanged(ListSelectionEvent e) {
          int row= table.getSelectedRow();
          if(row>=0)
          {
           txtMadoibong.setText(table.getValueAt(row,0).toString().trim());
           txtTendoibong.setText(table.getValueAt(row,1).toString().trim());
           cmbHLV.setSelectedItem(table.getValueAt(row,3));
           cmbSan.setSelectedItem(table.getValueAt(row, 4));
          }
    }
  }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDangky;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnTrove;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cmbHLV;
    private javax.swing.JComboBox cmbSan;
    private javax.swing.JPanel dtgDoibong;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtMadoibong;
    private javax.swing.JTextField txtTendoibong;
    // End of variables declaration//GEN-END:variables
}
